package com.leyou.listener;

import com.leyou.Service.GoodHtmlService;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class GoodsListener {


    @Autowired
    private GoodHtmlService goodHtmlService;

    /*
        新增和修改都是创建，覆盖之前生成过的静态页面
     */
    @RabbitListener(bindings = @QueueBinding(
            // 定义leyou.create.web.queue  队列
            value = @Queue(value = "leyou.create.web.queue", durable = "true"),
            exchange = @Exchange(
                    value = "leyou.item.exchange",
                    ignoreDeclarationExceptions = "true",
                    type = ExchangeTypes.TOPIC),
            key = {"item.insert", "item.update"}))
    public void listenerCreate(Long id) {
        if (id == null) {
            return;
        }
        // 创建新的商品静态页，也是更新覆盖
        this.goodHtmlService.asyncExcute(id);
    }

    /*
        删除静态页的方法
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "leyou.delete.web.queue", durable = "true"),
            exchange = @Exchange(
                    value = "leyou.item.exchange",
                    ignoreDeclarationExceptions = "true",
                    type = ExchangeTypes.TOPIC),
            key = "item.delete"))
    public void listenerDelete(Long id) {
        if (id == null) {
            return;
        }
        // 删除页面
        this.goodHtmlService.deleteHtml(id);
    }
}
